//
//  TLCommentCell.m
//  Coding_iOS
//
//  Created by taolei-mac on 2017/11/29.
//  Copyright © 2017年 com.lianchuan. All rights reserved.
//

#import "TLCommentCell.h"
#import "TTTAttributedLabel.h"
#import "TLCommentFrame.h"
#import "TLComment.h"
#import "TLOwner.h"
#import "TLHtmlMediaItem.h"

@interface TLCommentCell ()

@property (nonatomic, weak) TTTAttributedLabel *contentLabel;

@property (nonatomic, weak) UILabel *commentUserNameLabel;

@property (nonatomic, weak) UIButton *commentTimeButton;

@property (nonatomic, weak) UIImageView *splitlineImageView;

@end

@implementation TLCommentCell

- (void)setHideSplitline:(BOOL)hideSplitline
{
    _hideSplitline = hideSplitline;
    self.splitlineImageView.hidden = hideSplitline;
}

- (void)setCommentFrame:(TLCommentFrame *)commentFrame
{
    _commentFrame = commentFrame;
    TLComment *comment = commentFrame.comment;
    
    self.contentLabel.frame = commentFrame.contentFrame;
    self.contentLabel.text = comment.content;
    for (TLHtmlMediaItem *item in comment.htmlMedia.mediaItems) {
        if (item.displayStr.length > 0 && item.href.length > 0) {
            [self.contentLabel addLinkToTransitInformation:[NSDictionary dictionaryWithObject:item forKey:@"value"] withRange:item.range];
        }
    }
    
    self.commentUserNameLabel.text = comment.owner.name;
    [self.commentUserNameLabel sizeToFit];
    CGRect userFrame = commentFrame.userFrame;
    userFrame.size.width = self.commentUserNameLabel.width;
    self.commentUserNameLabel.frame = userFrame;
    
    [self.commentTimeButton setTitle:comment.created_at forState:UIControlStateNormal];
    [self.commentTimeButton.titleLabel sizeToFit];
    CGFloat timeW = self.commentTimeButton.titleLabel.width + 5 + self.commentTimeButton.currentImage.size.width;
    self.commentTimeButton.frame = CGRectMake(CGRectGetMaxX(self.commentUserNameLabel.frame) + 10, self.commentUserNameLabel.y, timeW, 13);
    
    self.splitlineImageView.frame = CGRectMake(10, commentFrame.cellHeight - self.splitlineImageView.image.size.height, TLScreenW - 40, self.splitlineImageView.image.size.height);
}

+ (instancetype)createCommentCellWithTableView:(UITableView *)tableView
{
    static NSString *Id = @"comment";
    TLCommentCell *cell = [tableView dequeueReusableCellWithIdentifier:Id];
    if(!cell){
        
        cell = [[TLCommentCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:Id];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.backgroundColor = [UIColor colorWithHexString:@"0xF2F4F6"];
    return cell;
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if(self){
        
        // 添加内容标签控件
        TTTAttributedLabel *contentLabel = [[TTTAttributedLabel alloc] initWithFrame:CGRectMake(5, 0, TLScreenW - 40 - 10, 20)];
        contentLabel.numberOfLines = 0;
        contentLabel.font = [UIFont systemFontOfSize:14];
        contentLabel.textColor = [UIColor colorWithHexString:@"0x425063"];
        contentLabel.linkAttributes = kLinkAttributes;
        contentLabel.activeLinkAttributes = kLinkAttributesActive;
        [self.contentView addSubview:contentLabel];
        self.contentLabel = contentLabel;
        
        // 发表评论人的名称
        UILabel *commentUserNameLabel = [[UILabel alloc] init];
        commentUserNameLabel.font = [UIFont systemFontOfSize:12];
        commentUserNameLabel.textColor = [UIColor colorWithHexString:@"0x76808E"];
        [self.contentView addSubview:commentUserNameLabel];
        self.commentUserNameLabel = commentUserNameLabel;
        
        // 发布评论时间
        UIButton *commentTimeButton = [UIButton buttonWithType:UIButtonTypeCustom];
        commentTimeButton.titleLabel.font = [UIFont systemFontOfSize:12];
        [commentTimeButton setTitleColor:[UIColor colorWithHexString:@"0x76808E"] forState:UIControlStateNormal];
        [commentTimeButton setImage:[UIImage originImageWithName:@"time_clock_icon"] forState:UIControlStateNormal];
        [commentTimeButton setImageEdgeInsets:UIEdgeInsetsMake(0, 0, 0, 5)];
        [self.contentView addSubview:commentTimeButton];
        self.commentTimeButton = commentTimeButton;
        
        // 分割线
        UIImageView *splitlineImageView = [[UIImageView alloc] initWithImage:[UIImage originImageWithName:@"splitlineImg"]];
        [self.contentView addSubview:splitlineImageView];
        self.splitlineImageView = splitlineImageView;
    }
    return self;
}

+ (CGFloat)commemtCellHeightWithCommemt:(TLComment *)comment
{
    CGSize resultSize = CGSizeZero;
    if (comment.content.length <= 0) {
        comment.content = @"  ";
    }
    CGSize maxSize = CGSizeMake(TLScreenW - 40, 200);
    NSMutableParagraphStyle *style = [NSMutableParagraphStyle new];
    style.lineBreakMode = NSLineBreakByWordWrapping;
    resultSize = [comment.content boundingRectWithSize:CGSizeMake(maxSize.width, maxSize.height)//用相对小的 width 去计算 height / 小 heigth 算 width
                                               options:(NSStringDrawingUsesLineFragmentOrigin)
                                            attributes:@{NSFontAttributeName: [UIFont systemFontOfSize:14], NSParagraphStyleAttributeName: style}
                                               context:nil].size;

    return resultSize.height + 10 + 10 + 10 + 13;
}

@end
